package com.ali.Reggie.controller;

import com.ali.Reggie.common.R;
import com.ali.Reggie.dto.DishDto;
import com.ali.Reggie.entity.Category;
import com.ali.Reggie.entity.Dish;
import com.ali.Reggie.entity.DishFlavor;
import com.ali.Reggie.service.CategoryService;
import com.ali.Reggie.service.DishFlavorService;
import com.ali.Reggie.service.DishService;
import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author ：ALi
 * @date ：Created in 2022/6/9 16:24
 * @description：菜品+菜品口味管理类
 * @modified By：
 * @version: $
 */
@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;

    //查询
    @GetMapping("/page")
    public R<Page> getAll(int page,int pageSize,String name){
        Page<Dish> pageInfo = new Page<>(page,pageSize);
        Page<DishDto> dishDtoPage = new Page<>(page,pageSize);
        //构建条件构造器
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        //设置查询条件
        queryWrapper.like(StringUtils.isNotEmpty(name), Dish::getName,name);
        //添加排序条件
        queryWrapper.orderByAsc(Dish::getName);
        pageInfo = dishService.page(pageInfo, queryWrapper);
        //将菜品id转换为菜品名称
        BeanUtils.copyProperties(pageInfo,dishDtoPage,"records");

        List<Dish> records = pageInfo.getRecords();
        List<DishDto> list = new ArrayList<>();
        for (Dish record:records) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(record,dishDto);

            Long categoryId = record.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if(category != null){
                String categoryName = category.getName();
                dishDto.setCategoryName(categoryName);
            }
            list.add(dishDto);
        }

        dishDtoPage.setRecords(list);
        return R.success(dishDtoPage);
    }

    //修改。此处是res风格的
    @GetMapping("/{id}")
    public R<DishDto> updateByID(@PathVariable  Long id){
        DishDto dishDto = dishService.getByIDWithFlavor(id);
        return R.success(dishDto);
    }


    //新增的保存按钮
    @PostMapping()
    public R<String> save(@RequestBody DishDto dishDto){
        dishService.saveWithFlavor(dishDto);

        return R.success("保存成功");

    }
    //修改的保存按钮
    @PutMapping()
    public R<String> UpdateAfterSave(@RequestBody DishDto dishDto){
        dishService.UpdateWithFlavorByID(dishDto);
        return R.success("修改成功");
    }

    //删除
    @DeleteMapping()
    public R<String> deleteByIDBach(@RequestParam("ids") List<Long> ids){
        dishService.removeByIds(ids);
        return R.success("删除成功");
    }

    //批量启售和停售
    @PostMapping("/status/{status}")
    public R<String> deleteByIDBach(@PathVariable("status") Integer status,@RequestParam("ids") List<Long> ids){
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(ids != null,Dish::getId,ids);
        List<Dish> dishList = dishService.list(lambdaQueryWrapper);
        if(dishList != null){
            for (Dish dish:dishList) {
                dish.setStatus(status);
                dishService.updateById(dish);
            }
            return R.success("菜品的售卖状态已更改！");
        }
        return R.error("售卖状态不可更改,请联系管理员或客服！");
    }

    //根据条件查询对应的菜品数据
//    @GetMapping("/list")
//    public R<List<Dish>> getDishById(Dish dish){
//        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId());
//        //查询状态为1的
//        lambdaQueryWrapper.eq(Dish::getStatus,1);
//        lambdaQueryWrapper.orderByAsc(Dish::getSort).orderByAsc(Dish::getUpdateTime);
//        List<Dish> dishes = dishService.list(lambdaQueryWrapper);
//        return R.success(dishes);
//    }

    //客户端使用的时候需要
    //修改根据条件查询对应的菜品数据，要包含口味数据，
    @GetMapping("/list")
    public R<List<DishDto>> getDishById(Dish dish){

        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId());
        //查询状态为1的
        lambdaQueryWrapper.eq(Dish::getStatus,1);
        lambdaQueryWrapper.orderByAsc(Dish::getSort).orderByAsc(Dish::getUpdateTime);
        List<Dish> dishes = dishService.list(lambdaQueryWrapper);

        //上面的是查到菜品信息，其他信息都没有
        List<DishDto> dishDtoList = new ArrayList<>();
        for (Dish dish1:dishes) {
            DishDto dishDto1 = new DishDto();
            BeanUtils.copyProperties(dish1,dishDto1);
            //得到分类id
            Long categoryId = dish1.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if(category != null){
                String name = category.getName();
                dishDto1.setCategoryName(name);
            }

            //菜品id
            Long id = dish1.getId();
            LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper1.eq(DishFlavor::getDishId,id);
            List<DishFlavor> list = dishFlavorService.list(lambdaQueryWrapper1);
            dishDto1.setFlavors(list);
            dishDtoList.add(dishDto1);
        }

        return R.success(dishDtoList);
    }



}